package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.GetUserTestScoreResq;
import com.ycsj.goldmedalnewconcept.bean.ScoreListInfo;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

public class ScoreSerchActivity extends BaseJpStarActivity implements OnClickListener {
	private TextView tv_main_back;
	private TextView tv_title_main;
	private ListView lv_score;
	MyAdapter adapter = new MyAdapter();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_search);
		
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}
		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintResource(R.color.base_color);
		
		init();
	}

	private void init() {
		tv_main_back = (TextView) findViewById(R.id.tv_main_back);
		tv_title_main = (TextView) findViewById(R.id.tv_title_main);
		lv_score = (ListView) findViewById(R.id.lv_score);
		
		tv_title_main.setText("我的成绩");
		tv_main_back.setOnClickListener(this);
		
		AssetManager mgr=getAssets();//得到AssetManager
//		Typeface tf=Typeface.createFromAsset(mgr, "fonts/title.TTF");//根据路径得到Typeface
//		tv_title_main.setTypeface(tf);//设置字体
//		tv_main_back.setTypeface(tf);
		
    	OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
//		final Request request = new Request.Builder().url(NetConstant.UNITTEST+"/GetUserTestScoreList.aspx?account="+SPUtils.getString(this,
//				ConfigConstant.USERNAME)+"&usertoken="+SPUtils.getString(this,ConfigConstant.USERTOKEN)).build();
    	final Request request = new Request.Builder().url(NetConstant.ycsj_platform+"?type=2&account="+SPUtils.getString(this,
				ConfigConstant.USERNAME)+"&usertoken="+SPUtils.getString(this,ConfigConstant.USERTOKEN)).build();
		
		
		// new call
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);
	}
	Handler handler=new Handler();
	Callback callBack=new Callback() {
		
		@Override
		public void onResponse(Response arg0) throws IOException {
			String json=arg0.body().string();
			Log.i("test", json);
			Gson gson = new Gson();
			 // 2 使用gson对象的方法
			GetUserTestScoreResq getUserTestScoreResq = gson.fromJson(json,GetUserTestScoreResq.class);
			//Toast.makeText(ScoreSerchActivity.this, getUserTestScoreResq.SCORE_LIST.get(0).EXAM_SCORE, 0).show();
			adapter.setInfos(getUserTestScoreResq.SCORE_LIST);
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					
					lv_score.setAdapter(adapter);
				}
			}, 10);
		}
		
		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub
			
		}
	};
	
	
	class MyAdapter extends BaseAdapter {
		
		ArrayList<ScoreListInfo> adapterNums=new ArrayList<ScoreListInfo>();

		public void setInfos(ArrayList<ScoreListInfo> adapterNums) {

			this.adapterNums = adapterNums;
			
			
			notifyDataSetChanged();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh=null;
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.items_score, null);

				vh = new ViewHolder();
				vh.iv_score_icon = (ImageView) convertView.findViewById(R.id.iv_score_icon);
				vh.iv_score = (ImageView) convertView.findViewById(R.id.iv_score);
				vh.tv_score_white = (TextView) convertView.findViewById(R.id.tv_score_white);
				vh.tv_score_uint = (TextView) convertView.findViewById(R.id.tv_score_uint);
				vh.tv_score_num = (TextView) convertView.findViewById(R.id.tv_score_num);
				vh.tv_score_date = (TextView) convertView.findViewById(R.id.tv_score_date);
				vh.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}
			
			vh.tv_score_white.setText(""+(position+1));
			vh.tv_score_uint.setText(adapterNums.get(position).GRADE_NAME+adapterNums.get(position).LESSON_NAME);
			vh.tv_score_num.setText("成绩 : "+adapterNums.get(position).EXAM_SCORE+"分");
			int TOTAL_TIME=Integer.parseInt(adapterNums.get(position).TOTAL_TIME);
			vh.tv_score_date.setText("时间 : "+TOTAL_TIME/60+"分"+TOTAL_TIME%60+"秒");
//			vh.tv_test_second.setText(list.get(position).LESSON_NAME);
			
			
			String date = adapterNums.get(position).EXAM_DT.substring(0, adapterNums.get(position).EXAM_DT.length()-8
					);
					
			
			vh.tv_date.setText(date);
			
			int[] icons={R.drawable.score_icon01,R.drawable.score_icon02,R.drawable.score_icon03};
//			Log.e("test", adapterNums.get(position).EXAM_TYPE_NAME);
//			if("课堂检测".equals(adapterNums.get(position).EXAM_TYPE_NAME)){
////				vh.iv_score_icon2.setVisibility(View.GONE);
//				Picasso.with(ScoreSerchActivity.this).load(R.drawable.score_icon02).fit().into(vh.iv_score_icon);
//				
//			}else{
//				//Toast.makeText(ScoreSerchActivity.this, "222", 0).show();
////				vh.iv_score_icon.setVisibility(View.GONE);
//				Picasso.with(ScoreSerchActivity.this).load(icons[0]).fit().into(vh.iv_score_icon);
//				
//			}
			
			if("课堂检测".equals(adapterNums.get(position).EXAM_TYPE_NAME)){
			Picasso.with(ScoreSerchActivity.this).load(icons[1]).fit().into(vh.iv_score_icon);
//
			}else if("语法检测".equals(adapterNums.get(position).EXAM_TYPE_NAME)){
				Picasso.with(ScoreSerchActivity.this).load(icons[2]).fit().into(vh.iv_score_icon);
			}
			
			
			int[] pics={R.drawable.score01,R.drawable.score02,R.drawable.score03,R.drawable.score04};
			String substring = adapterNums.get(position).GRADE_ID.substring(3);
			
			int i=Integer.parseInt(substring)-1;
			Picasso.with(ScoreSerchActivity.this).load(pics[i]).fit().into(vh.iv_score);
			 
			

			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(adapterNums.size()!=0&&adapterNums!=null){
				
				return adapterNums.size();
			}else{
				return 0;
			}
		}
	}
	
	class ViewHolder{
		public ImageView iv_score_icon;
		
		public ImageView iv_score;
		public TextView tv_score_white;
		public TextView tv_score_uint;
		public TextView tv_score_num;
		public TextView tv_score_date;
		public TextView tv_date;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_main_back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	
	@TargetApi(19) 
	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
	
}
